/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmConsultaNivel.java
 *
 * Created on 26/10/2009, 23:15:06
 */

package usuarios.telas;

import conexao.DAOException;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import nivelacesso.bd.NivelDao;
import nivelacesso.classes.Nivel;
import usuarios.bd.UsuarioDao;
import usuarios.classes.Usuarios;

/**
 *
 * @author Wolverine
 */

public class FrmCadastroUsuario extends javax.swing.JFrame {

    private Usuarios usuario;
    private List<Nivel> listaNivel;
    private NivelDao ndao;
    private UsuarioDao dao;


    /** Creates new form FrmConsultaNivel */
    public FrmCadastroUsuario( Usuarios usuario) throws DAOException {
        initComponents();
        URL url = this.getClass().getResource("/imagens/logo_mini.png");
        Image imagemTitulo = Toolkit.getDefaultToolkit().getImage(url);
        this.setIconImage(imagemTitulo);
        this.setLocationRelativeTo(null);
        try{
            UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        }catch (Exception e){
        }
        dao = new UsuarioDao();
        ndao = new NivelDao();
        listaNivel = ndao.consultarNivel();
        this.usuario = usuario;
        for(Nivel n : listaNivel){
            niveljComboBox.addItem(n);
        }
        if (this.usuario.getIdusuarios()!=null){
            this.usuario = new Usuarios();
           nomejTextField.setText(usuario.getNome());
        }
        setVisible(true);
    }

    




    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        nomejTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        SalvarjButton = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        niveljComboBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Usuários");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        jPanel1.add(nomejTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 100, -1));

        jLabel1.setText("Nome");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 3, -1, -1));

        jLabel2.setText("Nivel de Acesso");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 3, -1, -1));

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        SalvarjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/tick.png"))); // NOI18N
        SalvarjButton.setText("Salvar");
        SalvarjButton.setToolTipText("Salvar Usuário");
        SalvarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SalvarjButtonActionPerformed(evt);
            }
        });
        jPanel3.add(SalvarjButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 7, -1, -1));

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/cross.png"))); // NOI18N
        jButton4.setText("Fechar");
        jButton4.setToolTipText("Fechar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 7, -1, -1));

        jPanel1.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, 290, 40));

        jPanel1.add(niveljComboBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 20, 180, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 310, 100));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1cadastraProfessor(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1cadastraProfessor
        
}//GEN-LAST:event_jButton1cadastraProfessor

    private void jButton5voltarTela(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5voltarTela
        this.dispose();
}//GEN-LAST:event_jButton5voltarTela

    private void SalvarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SalvarjButtonActionPerformed
        try {
            if (usuario == null) {
                usuario.setSenha(nomejTextField.getText());
            }
            usuario.setNome(nomejTextField.getText());
            usuario.setNivelIdnivel((Nivel)niveljComboBox.getSelectedItem());
            dao.adcionarUsuario(usuario);
            JOptionPane.showMessageDialog(null,"Dados salvos com sucesso.");
            this.dispose();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Erro ao Salvar os Dados");
        }
    }//GEN-LAST:event_SalvarjButtonActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
    * @param args the command line arguments
    */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton SalvarjButton;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JComboBox niveljComboBox;
    private javax.swing.JTextField nomejTextField;
    // End of variables declaration//GEN-END:variables


}
